package me.yuxiaoyao.lock.annotation;

import me.yuxiaoyao.lock.LockExecutor;
import me.yuxiaoyao.lock.interceptor.DistributedLockAnnotateInterceptor;
import me.yuxiaoyao.lock.interceptor.DistributedLockOperationSource;
import me.yuxiaoyao.lock.interceptor.DistributedLockPointcutAdvisor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;


/**
 * @author kerryzhang on 2020/12/21
 */


@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(LockExecutor.class)
//@Import(DistributedLockRegistrar.class)
public class DistributedLockAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public AnnotationDistributedLockOperationSource distributedLockOperationSource() {
        return new AnnotationDistributedLockOperationSource();
    }


    @Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public DistributedLockAnnotateInterceptor distributedLockAnnotateInterceptor(DistributedLockOperationSource distributedLockOperationSource) {
        DistributedLockAnnotateInterceptor interceptor = new DistributedLockAnnotateInterceptor();
        interceptor.setDistributedLockOperationSource(distributedLockOperationSource);
        interceptor.configure(null, null, null);
        return interceptor;
    }


    @Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
    @Bean
    public DistributedLockPointcutAdvisor distributedLockPointcutAdvisor(DistributedLockAnnotateInterceptor interceptor) {
        DistributedLockPointcutAdvisor advisor = new DistributedLockPointcutAdvisor();

        advisor.setAdvice(interceptor);

        return advisor;
    }

}
